<?php

echo time();
$db['name'] = 'db_linkinfo';
$db['location'] = 'localhost';
$db['login'] = 'root';
$db['password'] = '';

switch ($_REQUEST['w']) {
    case '1': {
            $query_create_table[] = <<<EOD
        CREATE TABLE users (
        id INT(10) NOT NULL AUTO_INCREMENT,
        login VARCHAR(200) NOT NULL UNIQUE,
        password TEXT NOT NULL,
        email VARCHAR(200) NOT NULL UNIQUE,
        surname TEXT NOT NULL,
        lastname TEXT NOT NULL,
        isadmin BOOL NOT NULL,
        datetime_registration DATETIME NOT NULL,
        PRIMARY KEY (id)
        )
EOD;
            $query_create_table[] = <<<EOD
        CREATE TABLE users_session (
        id INT(10) NOT NULL AUTO_INCREMENT,
        id_users INT(10) NOT NULL,
        session TEXT NOT NULL,
        datetime_create DATETIME NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY (id_users) REFERENCES users(id) ON DELETE CASCADE
        )
EOD;
            $query_create_table[] = <<<EOD
        CREATE TABLE post (
        id INT(10) NOT NULL AUTO_INCREMENT,
        id_users INT(10) NOT NULL,
        title TEXT NOT NULL,
        img_path TEXT,
        text TEXT,
        datetime_create DATETIME NOT NULL,
        PRIMARY KEY (id),
        FOREIGN KEY (id_users) REFERENCES users(id) ON DELETE CASCADE
        )
EOD;

            $query_creata_db = <<<EOD
        CREATE DATABASE {$db['name']};
EOD;
            $dbcnx = @mysql_connect($db['location'], $db['login'], $db['password']);
            if (!$dbcnx) {
                echo( "<P>V nastojawi moment server bazi dannix ne dostypen.</P>" );
                echo mysql_error();
                exit();
            }

            if (mysql_query($query_creata_db)) {
                echo( "<P>Baza dannix '{$db['name']}' yspewno sozdana.</P>" );
            } else {
                echo("<P>Owibka pri sozdanie bazi dannix '{$db['name']}'.</P>");
                echo mysql_error();
            }


            if (!@mysql_select_db($db['name'], $dbcnx)) {
                echo( "<P>V nastojawi moment baza dannix '{$db['name']}' ne dostypna.</P>" );
            }

            foreach ($query_create_table as $query) {
                if (mysql_query($query)) {
                    echo( "<P>Tablica yspewno sozdana.</P>" );
                } else {
                    echo("<P>Owibka pri sozdani tablici</P>");
                    echo mysql_error();
                }
            }
        }
        break;

    case '2': {
            $query_drop_db = <<<EOD
            DROP DATABASE {$db['name']};
EOD;
            $dbcnx = @mysql_connect($db['location'], $db['login'], $db['password']);
            if (!$dbcnx) {
                echo( "<P>V nastojawi moment server bazi dannix ne dostypen.</P>" );
                echo mysql_error();
                exit();
            }

            if (mysql_query($query_drop_db)) {
                echo( "<P>Baza yspewno dropnyta.</P>" );
            } else {
                echo("<P>Owibka pri dropani bazi.</P>");
                echo mysql_error();
            }
        }
        break;

    default:
        echo '<a href="install.php?w=1">INSTALL</a></br><a href="install.php?w=2">DROP</a> ';
}
?>